/**
 * Created by wangshaohua on 2017/3/29.
 */

var common_resource = "http://www.i-cambio.com";
var isLoadDynamicJs = false;
$(function () {
    cambioReady();
});

function cambioReady() {
    if (isLoadDynamicJs) {
        return;
    }
    isLoadDynamicJs = true;
    loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/config.js", function () {
    });
    try {
        loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/utils/jquery.cookie.js", function () {
        });
    } catch (e) {
        alert(e);
    }
    loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/utils.js", function () {
        try {
            loadOtherJs();
            start();
        } catch (e) {
            alert(e);
        }
    });
    loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/order.js", function () {
    });
}

function loadOtherJs() {
    var js = document.getElementsByTagName("script");
    for (var i = 0; i < js.length; i++) {
        if (js[i].src.indexOf("dynamicHtml/cambio-dynamic-js/CambioLoadJS.js") > 0 && js[i].src.indexOf("?") > 0) {
            var arrayTemp = new Array();
            arrayTemp = js[i].src.split('?');
            arrayTemp = arrayTemp[1].split('=');
            arrayTemp = arrayTemp[1].split(',');
            loadJSes(arrayTemp);
        }
    }
}

function getLoadJsUrl() {
    var js = document.getElementsByTagName("script");
    for (var i = 0; i < js.length; i++) {
        if (js[i].src.indexOf("dynamicHtml/cambio-dynamic-js/CambioLoadJS.js") > 0) {
            return js[i].src;
        }
    }
}

function loadJSes(arrayTemp) {
    for (var i = 0; i < arrayTemp.length; i++) {
        //if (arrayTemp[i] == 'order') {
        //    loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/order.js", function () {
        //    });
        //}
    }
}

function start() {
    if (navigator.userAgent.indexOf("MicroBenefits") > -1) {
        loadByBrowser("WEIBANG");
        return;
    }
    if (BrowserUtils.isWeChat()) {
        loadByBrowser("WECHAT");
        return;
    }
    if (BrowserUtils.isCdp()) {
        loadByBrowser("CDP");
        return;
    }

    loadByBrowser("WEB");
    return;
}

function loadByBrowser(browserType) {
    //alert(browserType);
    switch (browserType) {
        case "WEIBANG":
            loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/weiBang.js", function () { //加载,并执行回调函数
                startCambioWeiBang();
            });
            break;
        case "WECHAT":
            loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/weChat.js", function () {
                //startCambioWeChatLogin();
                //startCambioWeChatShare(weChatConfig);
            });
            break;
        case "CDP":
            loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/cdp.js", function () { //加载,并执行回调函数
                //startCambioWeiBang();
            });
            break;
        case "WEB":
            //loadScript(common_resource + "/file-system/dynamicHtml/cambio-dynamic-js/www/js/weiBang.js", function () { //加载,并执行回调函数
            //    startCambioWeiBang();
            //});
            break;
        default:
    }
}

/**
 * Created by wangshaohua on 2017/3/29.
 */
function loadScript(url, callback) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    if (typeof(callback) != "undefined") {
        if (script.readyState) {
            script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    callback();
                }
            };
        } else {
            script.onload = function () {
                callback();
            };
        }
    }
    script.src = url;
    document.body.appendChild(script);
}